package com.komodo.island.gdx.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.actors.Image;
import com.komodo.island.gdx.Art;
import com.komodo.island.gdx.KomodoIsland;
import com.komodo.island.gdx.Persistence;
import com.komodo.island.gdx.dialog.Dialog;

public class PuzzleScreen extends Screen {

	Dialog drawer;
	TextureRegion background;
	public TextureRegion visit;
	public TextureRegion website;

	@Override
	public void init(KomodoIsland komodoIsland, Texture texture) {
		// TODO Auto-generated method stub
		super.init(komodoIsland, texture);

		this.previousScreen = komodoIsland.mainMenuScreen;
		visit = new TextureRegion(komodoIsland.additionTexture, 480, 632, 128,
				32);
		website= new TextureRegion(Art.tulisanTexture, 832, 0, 200,
				60);

		background = new TextureRegion(texture, 480, 0, 480, 320);
		if (drawer == null)
			drawer = new Dialog(480, 320, true, this, texture, komodoIsland) {

				@Override
				public void setThePosition() {
					// TODO Auto-generated method stub

				}

				@Override
				public void setTheListener() {
					// TODO Auto-generated method stub

				}

				@Override
				public void init() {
					// TODO Auto-generated method stub

				}

				@Override
				public void addElements() {
					// TODO Auto-generated method stub

				}
			};

	}

	@Override
	public void onScreenRun() {
		// TODO Auto-generated method stub
		drawer.draw();

		drawer.getSpriteBatch().begin();
		drawer.getSpriteBatch().draw(background, 0, 0);
		
		
		int x = 0;
		for (int i = 0; i < 96; i++) {

			int y = i / 12;
			if (Persistence.piecesUnlocked[i])
				try {
					drawer.getSpriteBatch().draw(komodoIsland.puzzlePieces[i],
							x * 40, 280 - y * 40);

				} catch (Exception e) {

				}
			x++;
			if (x % 12 == 0) {
				x = 0;
			}

			// batch.draw(background, 0, 0);
			drawer.getSpriteBatch().draw(visit, 0, 0);
			drawer.getSpriteBatch().draw(website, 128, 0);
		}
		// batch.draw(komodoIsland.puzzlePieces[i], 0, 0);
		drawer.getSpriteBatch().end();

		if (Gdx.input.justTouched()) {
			exitScreen();
		}
	}

	@Override
	public void onScreenLoading() {
		// TODO Auto-generated method stub

	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		super.hide();
		try {
			if (Persistence.sound) {
				if (komodoIsland.puzzle.isPlaying()) {
					komodoIsland.puzzle.stop();
				}
			} else {

			}
		} catch (Exception e) {

		}
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		super.show();
		if (Persistence.sound) {
			if (komodoIsland.main.isPlaying()) {
				komodoIsland.main.stop();
			} else {

			}
			if (komodoIsland.puzzle.isPlaying()) {

			} else {
				komodoIsland.puzzle.play();
			}

		} else {
			komodoIsland.main.stop();
			if (komodoIsland.puzzle.isPlaying())
				komodoIsland.puzzle.stop();

		}
	}

	@Override
	public void cleanResScreen() {
		// TODO Auto-generated method stub
		super.cleanResScreen();
		drawer.dispose();
	}

}
